package com.mf.morefun.util;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.mf.morefun.MoreFunApplication;
import com.mf.morefun.bean.ResultBean;
import com.mf.morefun.bean.TestBean;
import com.mf.morefun.bean.TestRankBean;
import com.mf.morefun.constant.Configuration;
import com.mf.morefun.listener.MyRequestListener;
import com.mf.morefun.requestbean.RequestTestBean;
import com.mf.morefun.requestbean.RequestTestRankBean;
import com.mf.morefun.ui.moreread.DoTestActivity;
import com.mf.morefun.ui.moreread.TestActivity;
import com.mf.morefun.widget.NetErrorPop;
import com.zhcx.zhcxlibrary.utils.DeviceUtils;
import com.zhcx.zhcxlibrary.utils.StringUtils;
import com.zhcx.zhcxlibrary.utils.ToastUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

public class HaveTestUtil {
    public void initGetTest(Context mContext, String bookId, String userId) {
        RequestParams params = new RequestParams(Configuration.HOST + Configuration.GETTEST);
        RequestTestBean requestTestBean = new RequestTestBean();
        RequestTestBean.MfTestBean mfTestBean = new RequestTestBean.MfTestBean();
        RequestTestBean.MfTestBean.BookBean bookBean = new RequestTestBean.MfTestBean.BookBean();
        bookBean.setId(bookId);
        mfTestBean.setBook(bookBean);
        requestTestBean.setMfTest(mfTestBean);
        params.setBodyContent(JSON.toJSONString(requestTestBean));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (null != MoreFunApplication.mTestList && MoreFunApplication.mTestList.size() > 0) {
                    MoreFunApplication.mTestList.clear();
                }
                if (!StringUtils.isEmpty(result)) {
                    ResultBean resultBean = JSON.parseObject(result, ResultBean.class);
                    if (resultBean.getErrorCode() == 0) {
                        if (!StringUtils.isEmpty(resultBean.getResult())) {
                            List<TestBean> testList = JSON.parseArray(resultBean.getResult(), TestBean.class);
                            if (null != MoreFunApplication.mTestList) {
                                MoreFunApplication.mTestList.addAll(testList);
                            }
                            if (null != testList && testList.size() > 0) {
                                if (null != MoreFunApplication.loginBean && null != MoreFunApplication.loginBean.getResult() && null != MoreFunApplication.loginBean.getResult().getOrgan()) {
                                    if (null != testList.get(0).getMfTest()) {
                                        String testId = testList.get(0).getMfTest().getId();
                                        initGetTestRank(mContext, testId, MoreFunApplication.loginBean.getResult().getOrgan().getId(), bookId, userId);
                                    }
                                }
                            }
                        } else {
                            ToastUtils.show(mContext, "该绘本还没有测试题！", 100);
                        }
                    } else {
                        ToastUtils.show(mContext, resultBean.getMessage(), 100);
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Message msg = new Message();
                msg.what = 101;
                msg.obj = mContext;
                mHandler.sendMessage(msg);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    //获取排行榜
    private void initGetTestRank(Context mContext, String testId, String orginId, String bookId, String userId) {
        RequestParams params = new RequestParams(Configuration.HOST + Configuration.TESTRANK);
        RequestTestRankBean requestTestRankBean = new RequestTestRankBean();
        RequestTestRankBean.TestBean testBean = new RequestTestRankBean.TestBean();
        testBean.setId(testId);
        RequestTestRankBean.OrganBean organBean = new RequestTestRankBean.OrganBean();
        organBean.setId(orginId);
        requestTestRankBean.setTest(testBean);
        requestTestRankBean.setOrgan(organBean);
        params.setBodyContent(JSON.toJSONString(requestTestRankBean));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (!StringUtils.isEmpty(result)) {
                    ResultBean resultBean = JSON.parseObject(result, ResultBean.class);
                    if (resultBean.getErrorCode() == 0) {
                        if (!StringUtils.isEmpty(resultBean.getResult())) {
                            List<TestRankBean> testRankBeanList = JSON.parseArray(resultBean.getResult(), TestRankBean.class);
                            if (null != testRankBeanList && testRankBeanList.size() > 0){
                                boolean isHave = false;
                                for (int i = 0; i < testRankBeanList.size(); i++){
                                    if (null != testRankBeanList.get(i).getUser()){
                                        if (userId.equals(testRankBeanList.get(i).getUser().getId())){
                                            isHave = true;
                                        }
                                    }
                                }
                                if (isHave){
                                    Intent mIntent = new Intent(mContext, TestActivity.class);
                                    mIntent.putExtra("BOOKID", bookId);
                                    mContext.startActivity(mIntent);
                                }else {
                                    Intent mIntent = new Intent(mContext, DoTestActivity.class);
                                    mIntent.putExtra("BOOKID", bookId);
                                    mContext.startActivity(mIntent);
                                }
                            }else {
                                Intent mIntent = new Intent(mContext, DoTestActivity.class);
                                mIntent.putExtra("BOOKID", bookId);
                                mContext.startActivity(mIntent);
                            }
                        } else {
                            Intent mIntent = new Intent(mContext, DoTestActivity.class);
                            mIntent.putExtra("BOOKID", bookId);
                            mContext.startActivity(mIntent);
                        }
                    } else {
                        ToastUtils.show(mContext, resultBean.getMessage(), 100);
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                mHandler.sendEmptyMessage(101);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 101:
                    NetErrorPop errorPop = new NetErrorPop((Context) msg.obj);
                    errorPop.showPop("");
                    break;
            }
        }
    };

}
